<template>
<div class="card-password-Edit views-container">
    <el-dialog :visible.sync="dialogVisibleImg" @close="clearHistory" width="35%" custom-class="reset-pwd wlm-form-dialog dialog-table">
        <div class="wlm-form">
            <div class="wlm-form-header wlm-dialog-fixheader">
                <span class="wlm-dialog-inlenr">编辑串码</span>
            </div>
            <div class="wlm-form-content">
                <div class="wlm-dialog-main">
                    <div class="wlm-dialog-content">
                        <el-form ref="user" :model="history" size="small" label-width="120px" class="retail-form" label-position="right">
                            <!-- <el-form-item label="核销码状态">
                                <el-radio-group v-model="formFormatData.formData.kammi_class">
                                    <el-radio :label="1">待出售</el-radio>
                                    <el-radio :label="2">仓库中</el-radio>
                                </el-radio-group>
                            </el-form-item> -->
                            <el-form-item label="核销码">
                                <el-input v-model.trim="history.vercode" maxlength="9">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="备注">
                                <el-input placeholder="请输入核销码备注" v-model.trim="history.explain">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="更新范围">
                                <el-radio-group v-model="history.range">
                                    <el-radio label="one">仅本条</el-radio>
                                    <el-radio label="all">同备注</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-form>
                    </div>
                </div>
            </div>
        </div>
        <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
            <el-button size="small" @click="saveDialogForm" type="primary">保存</el-button>
        </footer>
    </el-dialog>
    <div class="wlm-form">
        <div class="wlm-form-header">卡密库信息</div>
        <div class="wlm-form-content">
            <el-form ref="active" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="144px" class="retail-form" label-position="right">
                <el-form-item label="卡密库名称">
                    <span>{{formFormatData.formData.kammi_title}}</span>
                </el-form-item>
                <el-form-item label="关联商品">
                    <span v-if="formFormatData.formData.goods">{{formFormatData.formData.goods[0]['goods_name']}}</span>
                </el-form-item>
            </el-form>
        </div>
    </div>
    <div class="wlm-form">
        <div class="wlm-form-header flex-row"><span>核销码数据</span>
            <p style="margin-left:10px;font-size:12px;color:rgb(153, 153, 153);">共{{formFormatData.formData.written_off+formFormatData.formData.written_no}}条数据，{{formFormatData.formData.written_no}}条待核销，{{formFormatData.formData.written_off}}条已核销</p>
        </div>
        <div class="wlm-form-content">
            <div class="wlm-table"  style="padding:0">
                <div class="wlm-table-hearder-btn">
                    <el-button size="small" type="primary" @click="$router.push({path:'/application/cardSerialCodeAdd',query: {kammi_id: $route.query.kammi_id}})">添加核销码</el-button>
                    <el-button size="small" type="success" icon="document" :data-query="JSON.stringify(tableFormatData.userTable.files)" v-exceldown="{path:'importKeyOut&',query: {kammi_id:$route.query.kammi_id}}">导出核销码</el-button>
                </div>
                <div class="wlm-table-header">
                    <el-form :model="tableFormatData.userTable.files" size="small" label-width="85px" class="retail-form" label-position="right">
                        <el-form-item label="" class="filter">
                            <el-form-item label="核销码：">
                                <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.userTable.files.vercode" placeholder="请输入核销码" style="width:160px;"></el-input>
                            </el-form-item>
                            <el-form-item label="时间选择：">
                                <el-date-picker style="width:360px;margin-right:0;" v-model="tableFormatData.userTable.files.time" value-format="timestamp" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']">
                                </el-date-picker>
                            </el-form-item>
                        </el-form-item>
                        <el-form-item label="" class="filter-btns">
                            <el-button type="primary" @click="filesSerch">筛选</el-button>
                            <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <div class="wlm-table-content">
                    <el-tabs v-model="tableFormatData.userTable.files.state" type="card" @tab-click="filesSerch">
                        <el-tab-pane :label="`待核销(${formFormatData.formData.written_no})`" name="2"></el-tab-pane>
                        <el-tab-pane :label="`已核销(${formFormatData.formData.written_off})`" name="1"></el-tab-pane>
                    </el-tabs>
                    <el-table :ref="tableFormatData.userTable.key" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                        <el-table-column type="selection">
                        </el-table-column>
                        <el-table-column prop="date" label="排序值"  min-width="45">
                            <template slot-scope="scope">
                                <el-popover trigger="hover" placement="top">
                                    <div class="flex-col">
                                        <div class="flex-row flex-align-c" style="margin-bottom:10px;">
                                            <el-input size="mini" v-model="scope.row.sort" style="margin-right:10px"></el-input>
                                            <el-button type="text" size="mini" @click="changeTableItem({ids:[scope.row.kammi_vercode_id],sort:scope.row.sort},tableFormatData.userTable.change.sort)">确定</el-button>
                                        </div>
                                    </div>
                                    <div slot="reference" class="name-wrapper">
                                        <div class="pointer"><span>{{ scope.row.sort }}</span>
                                            <svg-icon icon-class="editIcon" />
                                        </div>
                                    </div>
                                </el-popover>
                            </template>
                        </el-table-column>
                        <el-table-column  prop="zip" label="核销码" >
                            <template slot-scope="scope">
                                <span>{{scope.row.vercode}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column  prop="address" label="状态" >
                            <template slot-scope="scope">
                                <span v-show="scope.row.state=='1'">已核销</span>
                                <span v-show="scope.row.state=='2'">未核销</span>
                            </template>
                        </el-table-column>
                        <el-table-column  prop="zip" label="添加时间" >
                            <template slot-scope="scope">
                                <span>{{scope.row.create_time}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column  prop="zip" label="备注" >
                            <template slot-scope="scope">
                                <span>{{scope.row.explain}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column  label="操作" >
                            <template slot-scope="scope">
                                <div class="operation-group">
                                    <el-button v-if="scope.row.state=='2'" class="wlm-text" @click="editDialogForm(scope.row)" type="text">编辑</el-button>
                                    <div v-if="scope.row.state=='2'" class="btn-line"></div>
                                    <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.kammi_vercode_id)">删除</el-button>
                                    <div v-if="scope.row.is_no=='2'" class="btn-line"></div>
                                    <el-button v-if="scope.row.is_no=='2'" class="wlm-text" type="text" @click="changeTableItem({ids:[scope.row.kammi_vercode_id]},tableFormatData.userTable.change.stop)">废弃</el-button>
                                </div>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-content flex-row flex-justify-b flex-align-c">
                        <el-row class="pagination-btns">
                            <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.userTable.files.checkall" @change="toggleSelection">全部</el-checkbox>
                            <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                            <el-popover placement="top" width="160" v-model="visible">
                                <div class="flex-row flex-align-c">
                                    <el-input size="mini" style="margin-right:10px;" v-model="allSortValue"></el-input>
                                    <el-button type="text" size="mini" @click="allSort">确定</el-button>
                                </div>
                                <el-button class="right-8" :disabled="isGroup" size="mini" slot="reference">更改排序</el-button>
                            </el-popover>
                        </el-row>
                        <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
                        </el-pagination>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  kammiDetail,
  keyVercodeList,
  keyVercodeDestroy,
  keyVercodeSort,
  keyVercodeEdit,
  verCodeNo
} from '@/api/application'
import mixins from '@/mixins/mixins'
import exceldown from '@/directive/exceldown'
import ExportFileIn from '@/components/ExportFileIn/index'
export default {
  mixins: [mixins.getters('Form'), mixins.getters('Table')],
  name: 'CardPasswordEdit',
  components: {
    ExportFileIn
  },
  computed: {
  },
  directives: {
    exceldown
  },
  data() {
    return {
      allSortValue: '',
      visible: false,
      // historyKeyValue: [],
      // historyKammiField: [],
      // historyKeyId: '',
      history: {
        kammi_id: '',
        kammi_vercode_id: '',
        vercode: '',
        explain: '',
        range: 'one'
      },
      dialogVisibleImg: false,
      dialogIndex: -1,
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: keyVercodeList,
            delList: keyVercodeDestroy
            //   msgConfig: {
            //     del: {
            //       type: 'warning',
            //       msg: '此操作会删除用户的所有数据，名下所有平台内如有正在交易的订单、金额，删除后所产生的纠纷由服务商自身承担 如要删除请慎重操作！！'
            //     }
            //   }
          },
          tableData: [],
          files: {
            kammi_id: this.$route.query.kammi_id,
            state: '2',
            vercode: '',
            time: [],
            ids: [],
            checkall: '0',
            Recycle: '1',
            redirect: 'kammi_vercode_id'
          },
          change: {
            sort: {
              key: 'sort',
              api: keyVercodeSort
            },
            stop: {
              name: '废弃',
              key: 'stop',
              api: verCodeNo
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      },
      formFormatData: {
        key: 'user',
        api: {
          editForm: {
            api: kammiDetail,
            params: {
              id: ''
            },
            redirect: 'kammi_id'
          },
          submitForm: kammiDetail
        },
        formData: {
          kammi_title: '',
          goods: '',
          written_no: '',
          written_off: ''
        },
        rules: {
          password: [{
            required: true,
            message: '请输入核销密码',
            trigger: 'blur'
          }]
        }
      }
    }
  },
  methods: {
    allSort() {
      this.visible = true
      const result = []
      console.log(this.tableFormatData.userTable.files.ids)
      this.tableFormatData.userTable.files.ids.map(item => {
        result.push(item.kammi_vercode_id)
      })
      console.log(result)
      keyVercodeSort({ ids: result, sort: this.allSortValue }).then((response) => {
        const { data: { code, data, msg } } = response
        console.log(data)
        if (code === 1) {
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.visible = false
          this.filesSerch()
        } else {
          this.$message.error(msg)
        }
      })
    },
    changeTableCallBack({ event, params: { response } }) {
      if (event === 'del') {
        kammiDetail({ kammi_id: this.$route.query.kammi_id }).then((response) => {
          const {
            data: {
              data: msgData = []
            }
          } = response
          const filtersFormat = {}
          const paramsKey = Object.keys(msgData)
          const hasParamsKey = Object.keys(this['formFormatData']['formData'])
          paramsKey.map(item => {
            if (hasParamsKey.includes(item)) {
              filtersFormat[item] = msgData[item]
            }
          })
          Object.assign(this['formFormatData']['formData'], filtersFormat)
        })
      }
    },
    editDialogForm(scopeRow) {
      Object.assign(this.history, scopeRow)
      this.dialogVisibleImg = true
    },
    saveDialogForm() {
      const { kammi_id, kammi_vercode_id, vercode, explain, range } = this.history
      // var newobj = {}
      // newobj.kammi_id = kammi_id
      if (vercode.length < 9) {
        this.$message.error('核销码长度为9位数')
      } else {
        keyVercodeEdit({ kammi_id, kammi_vercode_id, vercode, explain, range }).then((response) => {
          const { data: { code, data, msg } } = response
          console.log(data)
          if (code === 1) {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.dialogVisibleImg = false
            this.filesSerch()
          } else {
            this.$message.error(msg)
          }
        })
      }
    },
    saveForm() {
      this.formFormatSubmit({ router: { type: 'replace', path: '/application/cardPassword', code: 1 } })
    },
    addField() {
      this.formFormatData.formData.kammi_field.push({})
    },
    clearHistory() {
      this.history = {
        kammi_id: '',
        kammi_vercode_id: '',
        vercode: '',
        explain: '',
        range: 'one'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.card-password-Edit {
    .logo {
        height: 40px;
        width: 40px;
        border-radius: 50%;
        vertical-align: middle;
        margin-right: 6px;
    }

    // /deep/ .el-table__header thead .is-leaf {
    //     background-color: #f2f2f2 !important;
    //     height: 32px;
    //     padding: 10px 0;
    // }

}
</style>
